<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clear"
    />
    <div v-if="meta.indexOf('UpdateEndDate') > -1" class="doumee_table_header">
      <el-button
        v-if="meta.indexOf('UpdateEndDate') > -1"
        v-preventReClick
        type="primary"
        @click="changeIndate()"
      >{{ $t('message.tzyxq') }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table doumee-element-tableb"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        align="center"
        type="selection"
        width="55"
      />
      <el-table-column
        align="center"
        :label="$t('message.memberInfo')"
        prop="memberInfo"
        min-width="250"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.hydj')"
        prop="memberType"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.hydj')"
        prop="lastBuy"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.syyxqz')"
        prop="endDate"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        :label="$t('message.caozuo')"
        min-width="100"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="meta.indexOf('UpdateEndDate') > -1"
              v-preventReClick
              type="text"
              @click="changeIndate(scope.row)"
            >{{ $t('message.tzyxq') }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="totalCount"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />

    <el-dialog
      :title="$t('message.tzyxq')"
      :visible.sync="indateModal"
      :close-on-click-modal="modalDialog"
      :close-on-press-escape="modalDialog"
      :before-close="IndateClose"
      width="600px"
    >
      <el-form label-width="auto" label-suffix="：">
        <el-form-item :label="$t('message.yxqzj')">
          <el-input v-model="indateData.updateDays" oninput="value=value.replace(/^(0+)|[^\d]+/g, '').slice(0, 5)" class="w200" />
          <span class="ml10">{{ $t('message.tian') }}</span>
          <div class="text_warning">{{ $t('message.xghyqxzj') }}</div>
        </el-form-item>
        <el-form-item :label="$t('message.tzbz')">
          <el-input v-model="indateData.remark" maxlength="200" resize="none" :autosize="{ minRows: 5, maxRows: 5}" type="textarea" :placeholder="$t('message.qayqsrbz')" class="w300" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="indateModal = false">{{ $t('message.qx') }}</el-button>
        <el-button :loading="subLoading" type="primary" @click="onSubmit">{{ $t('message.qd') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { wonderfulMemMonList, wonderChangeIndate } from '@/api'
import QueryForm from '@/components/QueryForm'
export default {
  components: {
    QueryForm
  },
  data() {
    return {
      filters: {},
      list: [],
      totalCount: 0,
      listLoading: false,
      pagination: {
        page: 1,
        rows: 10
      },
      meta: [],

      modalDialog: this.$store.getters.dialog,
      indateModal: false,
      subLoading: false,
      selList: [],
      activeSel: {},
      indateData: {},
      queryFormConfig: {
        formItems: [
          {
            filed: 'memberInfo',
            type: 'input',
            label: this.$t('message.memberInfo')
          }
        ],
        online: true
      }
    }
  },
  created() {
    this.getList()
    this.meta = this.$route.meta.buttons || []
  },
  methods: {
    onSubmit() {
      const { selList, activeSel, indateData } = this
      if (!indateData.updateDays) {
        return this.$message.warning(this.$t('message.qsrytzdyxqts'))
      }
      let ids = []
      if (activeSel.id) {
        ids = [activeSel.id]
      } else {
        ids = selList.map(i => i.id)
      }
      this.subLoading = true
      wonderChangeIndate({
        param: { ...indateData, ids }
      }).then(res => {
        this.subLoading = false
        if (res.errorCode !== '000000') return
        this.getList()
        this.indateModal = false
        this.$message.success(this.$t('message.yxqxgcg'))
      }, () => {
        this.subLoading = false
      })
    },
    handleSelectionChange(val) {
      this.selList = val
    },
    IndateClose() {
      this.indateModal = false
      this.activeSel = {}
    },
    changeIndate(val) {
      if (val && val.id) {
        this.activeSel = val
      } else {
        this.activeSel = {}
        if (this.selList.length === 0) {
          return this.$message.warning(this.$t('message.qxxzytzdhy'))
        }
      }

      this.indateData = {}
      this.indateModal = true
    },
    getList(page) {
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
      }
      this.listLoading = true
      wonderfulMemMonList({
        pagination,
        param: {
          ...filters,
          venueId: sessionStorage.getItem('venueId')
        }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.totalCount = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    clear() {
      this.filters = { }
      this.getList(1)
    },
    handleSizeChange(val) {
      this.pagination.rows = val
      this.getList()
    }
  }
}
</script>

<style>
</style>
